package com.framework.loippi.utils;

import com.framework.loippi.utils.dto.WeekDate;
import org.joda.time.DateTime;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Dateutil {
	public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
	public static final String MINUTE_PATTERN = "yyyy-MM-dd HH:mm";
	public static final String HOUR_PATTERN = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_PATTERN = "yyyy-MM-dd";
	public static final String MONTH_PATTERN = "yyyy-MM";
	public static final String YEAR_PATTERN = "yyyy";
	public static final String MINUTE_ONLY_PATTERN = "mm";
	public static final String HOUR_ONLY_PATTERN = "HH";


	public static long parseToDateLong(String s, String style) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
		simpleDateFormat.applyPattern(style);
		Date date = null;
		if (s == null || s.length() < 5) {
			return 0;
		}
		try {
			date = simpleDateFormat.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date.getTime();
	}

	/**
	 * 取得当前日期是多少周
	 * 
	 * @param date
	 * @return
	 */
	public static int getWeekOfYear(Date date) {
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setMinimalDaysInFirstWeek(7);
		c.setTime(date);
		return c.get(Calendar.WEEK_OF_YEAR);
	}

	/**
	 * 得到某一年周的总数
	 * 
	 * @param year
	 * @return
	 */
	public static int getMaxWeekNumOfYear(int year) {
		Calendar c = new GregorianCalendar();
		c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
		return getWeekOfYear(c.getTime());
	}

	/**
	 * 得到某年某周的第一天
	 * 
	 * @param year
	 * @param week
	 * @return
	 */
	public static Date getFirstDayOfWeek(int year, int week) {
		Calendar c = new GregorianCalendar();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, Calendar.JANUARY);
		c.set(Calendar.DATE, 1);
		Calendar cal = (GregorianCalendar) c.clone();
		cal.add(Calendar.DATE, week * 7);
		return getFirstDayOfWeek(cal.getTime());
	}

	/**
	 * 得到某年某周的最后一天
	 * 
	 * @param year
	 * @param week
	 * @return
	 */
	public static Date getLastDayOfWeek(int year, int week) {
		Calendar c = new GregorianCalendar();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, Calendar.JANUARY);
		c.set(Calendar.DATE, 1);
		Calendar cal = (GregorianCalendar) c.clone();
		cal.add(Calendar.DATE, week * 7);
		return getLastDayOfWeek(cal.getTime());
	}

	/**
	 * 取得指定日期所在周的第一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfWeek(Date date) {
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(date);
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
		return c.getTime();
	}

	/**
	 * 取得指定日期所在周的最后一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getLastDayOfWeek(Date date) {
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(date);
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
		return c.getTime();
	}

	/**
	 * 取得当前日期所在周的第一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfWeek() {
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(new Date());
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() - 1); // Monday
		return c.getTime();
	}

	/**
	 * 取得当前日期所在周的最后一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getLastDayOfWeek() {
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(new Date());
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
		return c.getTime();
	}

	public static String longTimeToString(Long time) {
		if (null == time || time <= 0) {
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
		// 前面的lSysTime是秒数，先乘1000得到毫秒数，再转为java.util.Date类型
		java.util.Date date = new Date(time);
		// Date date = new Date();
		// date.setTime(time);

		// Calendar cal = Calendar.getInstance();
		// cal.setTimeInMillis(time*1000);
		// Date date = cal.getTime();
		String sDateTime = sdf.format(date); // 得到精确到秒的表示：08/31/2006 21:08:00
		return sDateTime;

	}

	/**
	 * 获取当前时间 yyyyMMddHHmmss
	 * 
	 * @return String
	 */
	public static String getCurrTime() {
		Date now = new Date();
		SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		String s = outFormat.format(now);
		return s;
	}

	/**
	 * 获取当前时间之后两个小时： yyyyMMddHHmmss
	 * 
	 * @return String
	 */
	public static String getCurrTimeAfter() {
		Date now = new Date();
		SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		Calendar ca = Calendar.getInstance();
		ca.setTime(now);
		ca.add(Calendar.HOUR_OF_DAY, 2);
		String s = outFormat.format(ca.getTime());
		return s;
	}

	/**
	 * 日期相加减天数
	 * 
	 * @param date
	 *            如果为Null，则为当前时间
	 * @param days
	 *            加减天数
	 * @param includeTime
	 *            是否包括时分秒,true表示包含
	 * @return
	 * @throws ParseException
	 */
	public static Date dateAdd(Date date, int days, boolean includeTime) {
		if (date == null) {
			date = new Date();
		}
		if (!includeTime) {
			SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN);
			try {
				date = sdf.parse(sdf.format(date));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
			}
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, days);
		return cal.getTime();
	}

	/**
	 * 时间格式化成字符串
	 * 
	 * @param date
	 *            Date
	 * @param pattern
	 *            StrUtils.DATE_TIME_PATTERN || StrUtils.DATE_PATTERN，
	 *            如果为空，则为yyyy-MM-dd
	 * @return
	 */
	public static String dateFormat(Date date, String pattern) {
		if (StringUtil.isEmpty(pattern)) {
			pattern = DATE_PATTERN;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}

	/**
	 * 字符串解析成时间对象
	 * 
	 * @param dateTimeString
	 *            String
	 * @param pattern
	 *            StrUtils.DATE_TIME_PATTERN ||
	 *            StrUtils.DATE_PATTERN，如果为空，则为yyyy-MM-dd
	 * @return
	 * @throws ParseException
	 */
	public static Date dateParse(String dateTimeString, String pattern) {
		if (StringUtil.isEmpty(pattern)) {
			pattern = DATE_PATTERN;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try {
			return sdf.parse(dateTimeString);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 将日期时间格式成只有日期的字符串（可以直接使用dateFormat，Pattern为Null进行格式化）
	 * 
	 * @param dateTime
	 *            Date
	 * @return
	 * @throws ParseException
	 */
	public static String dateTimeToDateString(Date dateTime) throws ParseException {
		String dateTimeString = Dateutil.dateFormat(dateTime, DATE_TIME_PATTERN);
		return dateTimeString.substring(0, 10);
	}

	/**
	 * 当时、分、秒为00:00:00时，将日期时间格式成只有日期的字符串， 当时、分、秒不为00:00:00时，直接返回
	 * 
	 * @param dateTime
	 *            Date
	 * @return
	 * @throws ParseException
	 */
	public static String dateTimeToDateStringIfTimeEndZero(Date dateTime) throws ParseException {
		String dateTimeString = Dateutil.dateFormat(dateTime, DATE_TIME_PATTERN);
		if (dateTimeString.endsWith("00:00:00")) {
			return dateTimeString.substring(0, 10);
		} else {
			return dateTimeString;
		}
	}

	/**
	 * 将日期时间格式成日期对象，和dateParse互用
	 * 
	 * @param dateTime
	 *            Date
	 * @return Date
	 * @throws ParseException
	 */
	public static Date dateTimeToDate(Date dateTime) throws ParseException {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dateTime);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 时间加减小时
	 * 
	 * @param startDate
	 *            要处理的时间，Null则为当前时间
	 * @param hours
	 *            加减的小时
	 * @return Date
	 */
	public static Date dateAddHours(Date startDate, int hours) {
		if (startDate == null) {
			startDate = new Date();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(startDate);
		c.set(Calendar.HOUR, c.get(Calendar.HOUR) + hours);
		return c.getTime();
	}

	/**
	 * 时间加减分钟
	 * 
	 * @param startDate
	 *            要处理的时间，Null则为当前时间
	 * @param minutes
	 *            加减的分钟
	 * @return
	 */
	public static Date dateAddMinutes(Date startDate, int minutes) {
		if (startDate == null) {
			startDate = new Date();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(startDate);
		c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + minutes);
		return c.getTime();
	}

	/**
	 * 时间加减秒数
	 * 
	 * @param startDate
	 *            要处理的时间，Null则为当前时间
	 * @param minutes
	 *            加减的秒数
	 * @return
	 */
	public static Date dateAddSeconds(Date startDate, int seconds) {
		if (startDate == null) {
			startDate = new Date();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(startDate);
		c.set(Calendar.SECOND, c.get(Calendar.SECOND) + seconds);
		return c.getTime();
	}

	/**
	 * 时间加减天数
	 * 
	 * @param startDate
	 *            要处理的时间，Null则为当前时间
	 * @param days
	 *            加减的天数
	 * @return Date
	 */
	public static Date dateAddDays(Date startDate, int days) {
		if (startDate == null) {
			startDate = new Date();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(startDate);
		c.set(Calendar.DATE, c.get(Calendar.DATE) + days);
		return c.getTime();
	}

	/**
	 * 时间加减月数
	 * 
	 * @param startDate
	 *            要处理的时间，Null则为当前时间
	 * @param months
	 *            加减的月数
	 * @return Date
	 */
	public static Date dateAddMonths(Date startDate, int months) {
		if (startDate == null) {
			startDate = new Date();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(startDate);
		c.set(Calendar.MONTH, c.get(Calendar.MONTH) + months);
		return c.getTime();
	}

	/**
	 * 时间加减年数
	 * 
	 * @param startDate
	 *            要处理的时间，Null则为当前时间
	 * @param years
	 *            加减的年数
	 * @return Date
	 */
	public static Date dateAddYears(Date startDate, int years) {
		if (startDate == null) {
			startDate = new Date();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(startDate);
		c.set(Calendar.YEAR, c.get(Calendar.YEAR) + years);
		return c.getTime();
	}

	/**
	 * 时间比较（如果myDate>compareDate返回1，<返回-1，相等返回0）
	 * 
	 * @param myDate
	 *            时间
	 * @param compareDate
	 *            要比较的时间
	 * @return int
	 */
	public static int dateCompare(Date myDate, Date compareDate) {
		Calendar myCal = Calendar.getInstance();
		Calendar compareCal = Calendar.getInstance();
		myCal.setTime(myDate);
		compareCal.setTime(compareDate);
		return myCal.compareTo(compareCal);
	}

	/**
	 * 获取两个时间中最小的一个时间
	 * 
	 * @param date
	 * @param compareDate
	 * @return
	 */
	public static Date dateMin(Date date, Date compareDate) {
		if (date == null) {
			return compareDate;
		}
		if (compareDate == null) {
			return date;
		}
		if (1 == dateCompare(date, compareDate)) {
			return compareDate;
		} else if (-1 == dateCompare(date, compareDate)) {
			return date;
		}
		return date;
	}

	/**
	 * 获取两个时间中最大的一个时间
	 * 
	 * @param date
	 * @param compareDate
	 * @return
	 */
	public static Date dateMax(Date date, Date compareDate) {
		if (date == null) {
			return compareDate;
		}
		if (compareDate == null) {
			return date;
		}
		if (1 == dateCompare(date, compareDate)) {
			return date;
		} else if (-1 == dateCompare(date, compareDate)) {
			return compareDate;
		}
		return date;
	}

	/**
	 * 获取两个日期（不含时分秒）相差的天数，不包含今天
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws ParseException
	 */
	public static int dateBetween(Date startDate, Date endDate) throws ParseException {
		Date dateStart = dateParse(dateFormat(startDate, DATE_PATTERN), DATE_PATTERN);
		Date dateEnd = dateParse(dateFormat(endDate, DATE_PATTERN), DATE_PATTERN);
		return (int) ((dateEnd.getTime() - dateStart.getTime()) / 1000 / 60 / 60 / 24);
	}

	/**
	 * 获取两个日期（不含时分秒）相差的天数，包含今天
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws ParseException
	 */
	public static int dateBetweenIncludeToday(Date startDate, Date endDate) throws ParseException {
		return dateBetween(startDate, endDate) + 1;
	}

	/**
	 * 获取日期时间的年份，如2017-02-13，返回2017
	 * 
	 * @param date
	 * @return
	 */
	public static int getYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.YEAR);
	}

	/**
	 * 获取日期时间的月份，如2017年2月13日，返回2
	 * 
	 * @param date
	 * @return
	 */
	public static int getMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.MONTH) + 1;
	}

	/**
	 * 获取日期时间的第几天（即返回日期的dd），如2017-02-13，返回13
	 * 
	 * @param date
	 * @return
	 */
	public static int getDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.DATE);
	}

	/**
	 * 获取日期时间当月的总天数，如2017-02-13，返回28
	 * 
	 * @param date
	 * @return
	 */
	public static int getDaysOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.getActualMaximum(Calendar.DATE);
	}

	/**
	 * 获取日期时间当年的总天数，如2017-02-13，返回2017年的总天数
	 * 
	 * @param date
	 * @return
	 */
	public static int getDaysOfYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.getActualMaximum(Calendar.DAY_OF_YEAR);
	}

	/**
	 * 根据时间获取当月最大的日期
	 * <li>2017-02-13，返回2017-02-28</li>
	 * <li>2016-02-13，返回2016-02-29</li>
	 * <li>2016-01-11，返回2016-01-31</li>
	 * 
	 * @param date
	 *            Date
	 * @return
	 * @throws Exception
	 */
	public static Date maxDateOfMonth(Date date) throws Exception {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int value = cal.getActualMaximum(Calendar.DATE);
		return dateParse(dateFormat(date, MONTH_PATTERN) + "-" + value, null);
	}

	/**
	 * 根据时间获取当月最小的日期，也就是返回当月的1号日期对象
	 * 
	 * @param date
	 *            Date
	 * @return
	 * @throws Exception
	 */
	public static Date minDateOfMonth(Date date) throws Exception {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int value = cal.getActualMinimum(Calendar.DATE);
		return dateParse(dateFormat(date, MONTH_PATTERN) + "-" + value, null);
	}

	/**
	 * 获取昨天的日期时间
	 * 
	 * @param date
	 *            Date
	 * @return
	 * @throws Exception
	 */
	public static Date getYesterday() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		return cal.getTime();
	}

	/**
	 * 判断时间是否超过今天 超过 返回 true
	 * 
	 * @return String
	 */
	public static boolean isTimeExpires(Date endDate) {
		Calendar now = Calendar.getInstance();
		Calendar auditTime = Calendar.getInstance();

		auditTime.clear();
		auditTime.setTime(endDate);

		if (auditTime.before(now)) {
			return false;

		}
		return true;
	}

	/**
	 * 两时间相减取得分钟数
	 * 
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static Long getMinuts(Date startTime, Date endTime) {
		Long millisecond = endTime.getTime() - startTime.getTime();
		Long minuts = millisecond / ((long) 1000 * 60);
		return minuts;
	}

	/**
	 * 获取本周开始时间
	 * 
	 * @return
	 */
	public static Date getBeginDayOfWeek() {
		Date date = new Date();
		if (date == null) {
			return null;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
		if (dayofweek == 1) {
			dayofweek += 7;
		}
		cal.add(Calendar.DATE, 2 - dayofweek);
		return cal.getTime();
	}

	/**
	 * 获取本周结束时间
	 * 
	 * @return
	 */
	public static Date getEndDayOfWeek() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getBeginDayOfWeek());
		cal.add(Calendar.DAY_OF_WEEK, 6);
		Date weekEndSta = cal.getTime();
		return weekEndSta;
	}

	public static String format(Date date) {
		if (null == date) {
			return "";
		}
		DateTime now = new DateTime();
		// 今天开始
		DateTime today_start = new DateTime(now.getYear(), now.getMonthOfYear(), now.getDayOfMonth(), 0, 0, 0);
		// 获取今天结束时间,明天开始时间
		DateTime today_end = today_start.plusDays(1);
		// 昨天开始时间
		DateTime yesterday_start = today_start.minusDays(1);
		// 明天结束时间/后天开始时间
		DateTime tomorrow_end = today_start.plusDays(2);
		if (date.after(today_start.toDate()) && date.before(today_end.toDate())) {
			return new DateTime(date).toString("HH:mm");
		} else if (date.after(yesterday_start.toDate()) && date.before(today_start.toDate()) || new DateTime(date).compareTo(today_start) == 0) {
			return String.format("昨天%s", new DateTime(date).toString("HH:mm"));
		} else if ((date.after(today_end.toDate()) && date.before(tomorrow_end.toDate())) || new DateTime(date).compareTo(today_end) == 0) {
			return String.format("明天%s", new DateTime(date).toString("HH:mm"));
		}
		return new DateTime(date).toString("yyyy-MM-dd HH:mm");
	}

	/**
	 * 获取待支付订单有效时间字符串
	 *		订单的支付有效时间默认为24小时
	 * @param startTime
	 * @return
	 */
	public static String getAdvancePayTime(Date startTime) {
		if (startTime == null) {
			startTime = new Date();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(startTime);
		c.set(Calendar.HOUR, c.get(Calendar.HOUR) + 24);
		Date endTime = c.getTime();
		Long millisecond = endTime.getTime() - startTime.getTime();
		Long minuts = millisecond / ((long) 1000 * 60);
		return "还剩"+ minuts/60L +"小时"+ minuts%60L +"分";
	}
	
	/**
	 * 获取售后订单关闭时间字符串
	 *		订单的支付有效时间默认为7天
	 * @param startTime
	 * @return
	 */
	public static String getCloseTime(Date startTime, Date updateTime) {
		if (startTime == null) {
			startTime = new Date();
		}
		
		long l=startTime.getTime()-updateTime.getTime();
		long day=l/(24*60*60*1000);
		long hour=(l/(60*60*1000)-day*24);
		long min=((l/(60*1000))-day*24*60-hour*60);
		//long s=(l/1000-day*24*60*60-hour*60*60-min*60);
		//System.out.println(""+day+"天"+hour+"小时"+min+"分"+s+"秒");
		
		return "还剩"+ day+"天"+hour+"小时"+min+"分";
	}

	public static String getAfterOrderCloseTime(Date applyTime) {
		Date now = new Date();
		Date date = dateAddDays(applyTime, 3);
		Long millisecond = date.getTime() - now.getTime();
		Long second = millisecond / (long) 1000;
		Long minuts = 0L;
		Long hours = 0L;
		Long day = 0L;
		if (second >= 60) {
			minuts = second / 60;
			if (minuts >= 60) {
				hours = minuts / 60;
				minuts = minuts % 60;
				if (hours > 24) {
					day = hours / 24;
					hours = hours % 24;
				}
			}
		}
		return "还剩"+ day +"天"+ hours +"小时"+ minuts +"分";
	}

	/**
	 * 获取两个相差年数
	 *
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws ParseException
	 */
	public static int getYears(Date startDate, Date endDate) {
		Calendar start = Calendar.getInstance();
		start.setTime(startDate);
		Calendar end = Calendar.getInstance();
		end.setTime(endDate);
		int year = end.get(Calendar.YEAR) - start.get(Calendar.YEAR);
		return year ;
	}

	/**
	 * 获取两个日期（含时分秒）相差的天数，不包含今天
	 *
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws ParseException
	 */
	public static int getDays(Date startDate, Date endDate) {
		Date dateStart = dateParse(dateFormat(startDate, DATE_TIME_PATTERN), DATE_TIME_PATTERN);
		Date dateEnd = dateParse(dateFormat(endDate, DATE_TIME_PATTERN), DATE_TIME_PATTERN);
		return (int) ((dateEnd.getTime() - dateStart.getTime()) / 1000 / 60 / 60 / 24);
	}

	/**
	 * 获取两个日期（含时分秒）相差的天数，不包含今天
	 *
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws ParseException
	 */
	public static int getDaysWithOutHours(Date startDate, Date endDate) {
		Date dateStart = dateParse(dateFormat(startDate, DATE_TIME_PATTERN), DATE_PATTERN);
		Date dateEnd = dateParse(dateFormat(endDate, DATE_TIME_PATTERN), DATE_PATTERN);
		return (int) ((dateEnd.getTime() - dateStart.getTime()) / 1000 / 60 / 60 / 24);
	}

	/**
	 * 获取小时
	 *
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws ParseException
	 */
	public static int getHours(Date startDate, Date endDate) {
		Date dateStart = dateParse(dateFormat(startDate, DATE_TIME_PATTERN), DATE_TIME_PATTERN);
		Date dateEnd = dateParse(dateFormat(endDate, DATE_TIME_PATTERN), DATE_TIME_PATTERN);
		return (int) ((dateEnd.getTime() - dateStart.getTime()) / 1000 / 60 / 60);
	}

	/**
	 * 获取分钟
	 *
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws ParseException
	 */
	public static int getMinutes(Date startDate, Date endDate) {
		Date dateStart = dateParse(dateFormat(startDate, DATE_TIME_PATTERN), DATE_TIME_PATTERN);
		Date dateEnd = dateParse(dateFormat(endDate, DATE_TIME_PATTERN), DATE_TIME_PATTERN);
		return (int) ((dateEnd.getTime() - dateStart.getTime()) / 1000 / 60);
	}

	/**
	 * 获取分钟(不带秒数)
	 *
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws ParseException
	 */
	public static int getMinutesWithoutSeconds(Date startDate, Date endDate) {
		Date dateStart = dateParse(dateFormat(startDate, MINUTE_PATTERN), MINUTE_PATTERN);
		Date dateEnd = dateParse(dateFormat(endDate, MINUTE_PATTERN), MINUTE_PATTERN);
		return (int) ((dateEnd.getTime() - dateStart.getTime()) / 1000 / 60);
	}

	/**
	 * 获取秒数
	 *
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws ParseException
	 */
	public static int getSeconds(Date startDate, Date endDate) {
		Date dateStart = dateParse(dateFormat(startDate, DATE_TIME_PATTERN), DATE_TIME_PATTERN);
		Date dateEnd = dateParse(dateFormat(endDate, DATE_TIME_PATTERN), DATE_TIME_PATTERN);
		return (int) ((dateEnd.getTime() - dateStart.getTime()) / 1000);
	}

	/**
	 * 获取两个日期时间 差的形式为：xxx分钟前
	 *
	 * @param startDate
	 * @param endDate
	 * @return
	 *
	 */
	public static String getTime(Date startDate, Date endDate) {
		int years = getYears(startDate, endDate);
		int days = getDaysWithOutHours(startDate, endDate);
		int hours = getHours(startDate, endDate);
		int minutes = getMinutes(startDate, endDate);
		int seconds = getSeconds(startDate, endDate);

		String format = dateFormat(startDate, MINUTE_PATTERN);
		String times = format.substring(11, format.length());
		String date = format.substring(0, 10);
		String[] dateSplit = date.split("-");
		String[] timeSplit = times.split(":");
		if (days < 0) {
			return timeSplit[0]+":"+timeSplit[1];
		}
		else if (days == 1) {
			return "昨天" +timeSplit[0]+":"+timeSplit[1];
		}
		else {
			return dateFormat(startDate, "yyyy/MM/dd");
		}
	}


	/**
	 * 获取指定时区的当前时间所在的本周所有日期
	 *
	 * @param args
	 * @throws ParseException
	 */
	public static List<WeekDate> getTimeInterval() {
		List<WeekDate> res = new ArrayList<WeekDate>();
		Calendar cal = Calendar.getInstance();
		Date dateNow = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
			cal.add(Calendar.DATE, -1);
		}
		WeekDate data = null;
		for (int i = 1; i <= 7; i++) {
			data = new WeekDate(i, sdf.format(cal.getTime()));
			cal.add(Calendar.DATE, 1);
			res.add(data);
		}
		return res;

	}

	public static String getWeekStr(int i, boolean sunDayisFirst) {
		if (sunDayisFirst && i == 0) {
			i = 7;
		}
		switch (i) {
			case 1:
				return "周一";
			case 2:
				return "周二";
			case 3:
				return "周三";
			case 4:
				return "周四";
			case 5:
				return "周五";
			case 6:
				return "周六";
			case 7:
				return "周日";
			default:
				return "";
		}
	}
	
	public static void main(String[] args) throws ParseException {
		getTime(dateParse("2019-09-22 15:35:52", DATE_PATTERN), dateParse("2019-09-23 10:42:00", DATE_PATTERN));

	}



}
